//  Date:         08/08/2018
//  task:         education analysis
//  project:      World Development

capture log close
clear all
macro drop _all
set more off
version 14
set matsize 800 

//  #0:  setup

log using Analysis_school_QuasiControl,replace text

//  #1: load data
use ubridge_schools_quasicontrol.dta

replace cluster_id=schoolid if cluster_id==.

//  #2: define outcomes
	
local edu_monitoring monitoring_index c_V_deo_ever_call c_V_insp_calls c_V_deo_visit_rec c_V_isp_visit_rec   
local edu_effort effort_index  c_Teacher_Absent c_V_present_teach_e c_V_perc_alotwritten c_V_perc_Engaged c_V_school_staff_meet
local edu_input input_index  c_V_n_teachers c_V_teach_transf_to c_students_supply1 c_students_supply2 c_students_supply3
	
********************************************************************************
*REGRESSIONS
********************************************************************************

foreach y in `edu_monitoring' `edu_effort' `edu_input' { 
	capture noisily reg `y'2 treat `y'1 i.district
	capture est sto `y'_m1

	capture noisily reg `y'3 treat `y'1 i.district
	capture est sto `y'_e1
	
***********************	
* regressions without baseline measure of the DV
***********************
	capture noisily reg `y'2 treat i.district
	capture est sto `y'_m2
	
	capture noisily reg `y'3 treat i.district
	capture est sto `y'_e2
	
	}	
		
*************************
* Short-term tbl
*************************

forval c = 1/2  { //*************************************************************

local one monitoring_index_m`c' c_V_deo_ever_call_m`c' c_V_insp_calls_m`c' c_V_deo_visit_rec_m`c' c_V_isp_visit_rec_m`c' 
local two effort_index_m`c' c_Teacher_Absent_m`c' c_V_present_teach_e_m`c' c_V_perc_alotwritten_m`c' c_V_perc_Engaged_m`c'	
local three input_index_m`c' c_V_n_teachers_m`c' c_V_teach_transf_to_m`c' c_students_supply1_m`c' c_students_supply2_m`c' c_students_supply3_m`c' 

	tempfile short
	local n = 0
	foreach i in one two three {

		suest ``i'' , vce(cluster cluster_id)
	
		preserve

			regsave , pval ci table(e1, parentheses(stderr) format(%8.3fc)) 
			gen grp = `n'
			keep if regexm(var , ":treat_coef") ///
				| regexm(var , ":treat_stderr") ///
				| regexm(var , ":treat_pval") ///
				| regexm(var , ":treat_ci_upper") ///
				| regexm(var , ":treat_ci_lower")
			
			replace var = subinstr(var , "treat_" , "" , .)
			gen stat = substr(var , strpos(var , ":") + 1 , .)
			replace var = substr(var , 1 , strpos(var , "_m`c'_mean") - 1)
			replace var = substr(var , 3 , .) if regexm(var , "^c_")
			
			gen order = _n
			reshape wide e1 order , i(var) j(stat) string
			renvars e1* , prefix(S)
			
			if `n' > 0 {
				append using `short'
				}
			
			save `short' , replace
		
		restore
		local ++n
		}


******************	
* long-term tbl
******************

local one monitoring_index_e`c' c_V_deo_ever_call_e`c' c_V_insp_calls_e`c' c_V_deo_visit_rec_e`c' c_V_isp_visit_rec_e`c' 
local two effort_index_e`c' c_Teacher_Absent_e`c' c_V_present_teach_e_e`c' c_V_perc_alotwritten_e`c' c_V_perc_Engaged_e`c' c_V_school_staff_meet_e`c'	
local three input_index_e`c' c_V_n_teachers_e`c' c_V_teach_transf_to_e`c' c_students_supply1_e`c' c_students_supply2_e`c' c_students_supply3_e`c' 

		tempfile long
		local n = 0
		foreach i in one two three {
		
			suest ``i'' , vce(cluster cluster_id)
			
			preserve
		
				regsave , pval ci table(e1, parentheses(stderr) format(%8.3fc)) 
		
				gen grp = `n'
				keep if regexm(var , ":treat_coef") ///
					| regexm(var , ":treat_stderr") ///
					| regexm(var , ":treat_pval") ///
					| regexm(var , ":treat_ci_upper") ///
					| regexm(var , ":treat_ci_lower")
					
				replace var = subinstr(var , "treat_" , "" , .)
				gen stat = substr(var , strpos(var , ":") + 1 , .)
				replace var = substr(var , 1 , strpos(var , "_e`c'_mean") - 1)
				replace var = substr(var , 3 , .) if regexm(var , "^c_")
	
				gen order = _n
				reshape wide e1 order , i(var) j(stat) string
				renvars e1* , prefix(L)

				if `n' > 0 {
					append using `long'
					}
			
				save `long' , replace
			
			restore
			local ++n
			}

	preserve
		use `short' , replace
		merge 1:1 var using `long'
		
		foreach k in S L {
			gen `k'_ci = "[" + `k'e1ci_lower + " , " + `k'e1ci_upper + "]" 
			replace `k'_ci = "" if missing(`k'e1ci_lower)

			destring `k'e1pval , replace
			replace `k'e1pval = `k'e1pval/2 ///
				if real(`k'e1coef) >= 0 & !missing(`k'e1pval)
			replace `k'e1pval = 1-(`k'e1pval/2) ///
				if real(`k'e1coef) < 0
			tostring `k'e1pval , replace format(%12.3f) force
			replace `k'e1pval = "" if `k'e1pval == "."

			}
		drop *lower *upper
		
		tempfile combine`c'
		save `combine`c'' , replace
restore
	
preserve	
		use `combine`c'' , clear
		compress

		gsort grp ordercoef -_merge
		include output3_replace_edit.do
		drop order* grp _merge
		order var Se1coef Se1stderr S_ci Se1pval  Le1coef Le1stderr L_ci Le1pval 
	saveold edu_QuasiControl`c', replace
restore
	} 


*************************************************************
* Table 17 in Supplementary Information
*************************************************************

preserve
use edu_QuasiControl1.dta, clear
list, sep(5)
restore

*************************************************************
* Weighted index
*************************************************************

//  #2: define outcomes
	
local edu_monitoring index_edu_monitoring c_V_deo_ever_call c_V_insp_calls c_V_deo_visit_rec c_V_isp_visit_rec   
local edu_effort index_edu_effort  c_Teacher_Absent c_V_present_teach_e c_V_perc_alotwritten c_V_perc_Engaged c_V_school_staff_meet
local edu_input index_edu_input  c_V_n_teachers c_V_teach_transf_to c_students_supply1 c_students_supply2 c_students_supply3
	
********************************************************************************
*REGRESSIONS
********************************************************************************

foreach y in `edu_monitoring' `edu_effort' `edu_input' { 
	capture noisily reg `y'2 treat `y'1 i.district
	capture est sto `y'_m1

	capture noisily reg `y'3 treat `y'1 i.district
	capture est sto `y'_e1
	
***********************	
* regressions without baseline measure of the DV
***********************
	capture noisily reg `y'2 treat i.district
	capture est sto `y'_m2
	
	capture noisily reg `y'3 treat i.district
	capture est sto `y'_e2
	
	}	
		
*************************
* Short-term tbl
*************************

forval c = 1/2  { //*************************************************************

local one index_edu_monitoring_m`c' c_V_deo_ever_call_m`c' c_V_insp_calls_m`c' c_V_deo_visit_rec_m`c' c_V_isp_visit_rec_m`c' 
local two index_edu_effort_m`c' c_Teacher_Absent_m`c' c_V_present_teach_e_m`c' c_V_perc_alotwritten_m`c' c_V_perc_Engaged_m`c'	
local three index_edu_input_m`c' c_V_n_teachers_m`c' c_V_teach_transf_to_m`c' c_students_supply1_m`c' c_students_supply2_m`c' c_students_supply3_m`c' 

	tempfile short
	local n = 0
	foreach i in one two three {

		suest ``i'' , vce(cluster cluster_id)
	
		preserve

			regsave , pval ci table(e1, parentheses(stderr) format(%8.3fc)) 
			gen grp = `n'
			keep if regexm(var , ":treat_coef") ///
				| regexm(var , ":treat_stderr") ///
				| regexm(var , ":treat_pval") ///
				| regexm(var , ":treat_ci_upper") ///
				| regexm(var , ":treat_ci_lower")
			
			replace var = subinstr(var , "treat_" , "" , .)
			gen stat = substr(var , strpos(var , ":") + 1 , .)
			replace var = substr(var , 1 , strpos(var , "_m`c'_mean") - 1)
			replace var = substr(var , 3 , .) if regexm(var , "^c_")
			
			gen order = _n
			reshape wide e1 order , i(var) j(stat) string
			renvars e1* , prefix(S)
			
			if `n' > 0 {
				append using `short'
				}
			
			save `short' , replace
		
		restore
		local ++n
		}

******************	
* long-term tbl
******************

local one index_edu_monitoring_e`c' c_V_deo_ever_call_e`c' c_V_insp_calls_e`c' c_V_deo_visit_rec_e`c' c_V_isp_visit_rec_e`c' 
local two index_edu_effort_e`c' c_Teacher_Absent_e`c' c_V_present_teach_e_e`c' c_V_perc_alotwritten_e`c' c_V_perc_Engaged_e`c' c_V_school_staff_meet_e`c'	
local three index_edu_input_e`c' c_V_n_teachers_e`c' c_V_teach_transf_to_e`c' c_students_supply1_e`c' c_students_supply2_e`c' c_students_supply3_e`c' 

		tempfile long
		local n = 0
		foreach i in one two three {
		
			suest ``i'' , vce(cluster cluster_id)
			
			preserve
		
				regsave , pval ci table(e1, parentheses(stderr) format(%8.3fc)) 
		
				gen grp = `n'
				keep if regexm(var , ":treat_coef") ///
					| regexm(var , ":treat_stderr") ///
					| regexm(var , ":treat_pval") ///
					| regexm(var , ":treat_ci_upper") ///
					| regexm(var , ":treat_ci_lower")
					
				replace var = subinstr(var , "treat_" , "" , .)
				gen stat = substr(var , strpos(var , ":") + 1 , .)
				replace var = substr(var , 1 , strpos(var , "_e`c'_mean") - 1)
				replace var = substr(var , 3 , .) if regexm(var , "^c_")
	
				gen order = _n
				reshape wide e1 order , i(var) j(stat) string
				renvars e1* , prefix(L)

				if `n' > 0 {
					append using `long'
					}
			
				save `long' , replace
			
			restore
			local ++n
			}

	preserve
		use `short' , replace
		merge 1:1 var using `long'
		
		foreach k in S L {
			gen `k'_ci = "[" + `k'e1ci_lower + " , " + `k'e1ci_upper + "]" 
			replace `k'_ci = "" if missing(`k'e1ci_lower)

			destring `k'e1pval , replace
			replace `k'e1pval = `k'e1pval/2 ///
				if real(`k'e1coef) >= 0 & !missing(`k'e1pval)
			replace `k'e1pval = 1-(`k'e1pval/2) ///
				if real(`k'e1coef) < 0
			tostring `k'e1pval , replace format(%12.3f) force
			replace `k'e1pval = "" if `k'e1pval == "."

			}
		drop *lower *upper
		
		tempfile combine`c'
		save `combine`c'' , replace
restore
	
preserve	
		use `combine`c'' , clear
		compress

		gsort grp ordercoef -_merge
		include output3_replace_edit.do
		drop order* grp _merge
		order var Se1coef Se1stderr S_ci Se1pval  Le1coef Le1stderr L_ci Le1pval 
	saveold edu_QuasiControlA`c', replace
restore
} 

*************************************************************
* Table 18 in the Supplementary Information
*************************************************************

preserve
use edu_QuasiControlA1.dta, clear
list, sep(5)	

restore

*************************************************************
* Randomization Inference: Anderson
*************************************************************
set more off
use ubridge_schools_quasicontrol.dta, clear

replace cluster_id=schoolid if cluster_id==.


forvalues j=2(1)3{
foreach i in  monitoring effort input  {
	regress index_edu_`i'`j' treat index_edu_`i'1 i.district
	mat beta=e(b)
	svmat double beta
	ren beta1 beta`i'`j'
	drop beta2-beta8
	su beta`i'`j'
	local beta`i'`j' = r(mean)
	ritest treat _b[ treat ], cluster(cluster_id) reps(5000) right seed(05062018) saving(`i'`j'QC, replace): regress index_edu_`i'`j' treat index_edu_`i'1 i.district, cl(cluster_id)

	preserve
			use `i'`j'QC, clear
			gen t= `beta`i'`j''
			gen c=0
			replace c=1 if _pm_1 >= t
			egen p = mean(c)
			saveold `i'`j'QC, replace
	restore
	}	
		}
	
*************************************************************
* Randomization Inference: Kling
*************************************************************
set more off

forvalues j=2(1)3{
foreach i in  monitoring_index effort_index input_index {
	regress `i'`j' treat `i'1 i.district
	mat beta=e(b)
	svmat double beta
	ren beta1 beta`i'`j'
	drop beta2-beta8
	su beta`i'`j'
	local beta`i'`j' = r(mean)
	ritest treat _b[ treat ], cluster(cluster_id) reps(5000) right seed(05026018) saving(`i'`j'QCK, replace): regress `i'`j' treat `i'1 i.district, cl(cluster_id)

	preserve
			use `i'`j'QCK, clear
			gen t= `beta`i'`j''
			gen c=0
			replace c=1 if _pm_1 >= t
			egen p = mean(c)
			saveold `i'`j'QCK, replace
	restore
	}		
	 }


graph drop _all
preserve 

foreach i in monitoring effort input {
	use `i'_index2QCK.dta, clear
	quietly su t, de
	local t=round(r(mean),.001)
	quietly su p, de
	local p=round(r(mean),.001)

	hist _pm_1 , kdensity lcolor() title(`i' (year-1)) addplot(pci 0 `t' 3 `t', lc(red) lp(solid) lw(thick)) text(1.5 .4 "pval=`p'", size(small)) text(2.5 `t' "{&tau}=`t'",place(nw) size(small)) name(`i'2) xlabel(-.7(.1).7) xsca(r(-.7 .7)) legend(off)
	}
restore 	

preserve 
foreach i in monitoring effort input {
	use `i'_index3QCK.dta, clear
	quietly su t, de
	local t=round(r(mean),.001)
	quietly su p, de
	local p=round(r(mean),.001)

	hist _pm_1 , kdensity lcolor() title(`i' (year-2)) addplot(pci 0 `t' 3 `t', lc(red) lp(solid) lw(thick)) text(1.5 .4 "pval=`p'", size(small)) text(2.5 `t' "{&tau}=`t'",place(nw) size(small)) name(`i'3) xlabel(-.7(.1).7) xsca(r(-.7 .7)) legend(off)
	}
restore 

	graph combine monitoring2 monitoring3 effort2 effort3 input2 input3, title("Education (unweighted indices, quasi-control)") col(2) // Graph not included in Supplementary Information
	
log close
clear
exit